package com.hjx.movie.core.system.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjx.movie.core.system.runner.SystemStaticData;
import com.hjx.movie.core.system.service.admin.ICinemaService;
import com.hjx.movie.persistence.system.mapper.IvyCinemaMapper;
import com.hjx.movie.persistence.system.model.IvyCinema;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>Title:</p>
 * <p>Description:</p>
 *
 * @author hjx
 * @date 2021/2/7 - 21:50
 */
@Service
@Transactional
@Slf4j
@CacheConfig(cacheNames = "IvyMovie:SpringCache:CinemaCache",keyGenerator = "cacheKeyGenerator")
public class CinemaService extends ServiceImpl<IvyCinemaMapper, IvyCinema> implements ICinemaService {
    @Resource
    private SystemStaticData staticData;

    @Cacheable(unless="#result == null")
    @Override
    public List<IvyCinema> findAllCinema() {
        return list();
    }

    @Cacheable(unless="#result == null")
    @Override
    public List<IvyCinema> findAllCinema(Wrapper<IvyCinema> wrapper) {
        return list(wrapper);
    }

    @Cacheable(unless="#result == null")
    @Override
    public IvyCinema findCinemaById(Long id) {
        return getById(id);
    }

    @Override
    public List<IvyCinema> findCinemaByCondition(Long areaId, String cinemaNameLike, Integer cinemaStatus) {
        if (areaId.equals(1L)){
            QueryWrapper<IvyCinema> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(cinemaNameLike)){
                wrapper.like("cinema_name",cinemaNameLike);
            }
            if (cinemaStatus!=null){
                wrapper.eq("status",cinemaStatus);
            }else {
                wrapper.ne("status",staticData.getStaticValue("STATUS_DELETE"));
            }
            return list(wrapper);
        }
        return getBaseMapper().findCinemaByCondition(areaId,cinemaNameLike,cinemaStatus);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean addCinema(IvyCinema cinema) {
        return save(cinema);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean realDeleteCinema(Long cinemaId) {
        return removeById(cinemaId);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean updateCinema(IvyCinema cinema) {
        return updateById(cinema);
    }

    @CacheEvict(value = "IvyMovie:SpringCache:CinemaCache",allEntries=true,beforeInvocation = true)
    @Override
    public boolean logicDeleteCinema(Long cinemaId) {
        log.debug("逻辑删除影院");
        return updateById(IvyCinema.builder()
                .cinemaId(cinemaId)
                .status(Integer.parseInt(staticData.getStaticValue("STATUS_DELETE")))
                .build());
    }
}
